package com.alibaba.nacos.naming.core.v2.pojo;

import com.alibaba.nacos.api.common.Constants;
import com.google.common.base.Joiner;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import java.util.concurrent.atomic.AtomicLong;

@RequiredArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Setter
@Getter
public class Service {
    @EqualsAndHashCode.Include
    private final String namespace;
    @EqualsAndHashCode.Include
    private final String group;
    @EqualsAndHashCode.Include
    private final String name;
    private final boolean ephemeral;

    private AtomicLong revision = new AtomicLong(0);
    private long lastUpdatedTime = System.currentTimeMillis();

    public static Service newService(String namespace, String group, String name) {
        return newService(namespace, group, name, true);
    }

    public static Service newService(String namespace, String group, String name, boolean ephemeral) {
        return new Service(namespace, group, name, ephemeral);
    }

    Joiner joiner = Joiner.on(Constants.SERVICE_INFO_SPLITER);
    public String getGroupedServiceName() {
        return joiner.join(group, name);
    }

    public void renewUpdateTime() {
        lastUpdatedTime = System.currentTimeMillis();
    }

    public void incrementRevision() {
        revision.incrementAndGet();
    }
}
